<?php
    $link = mysqli_connect("localhost","root","") or die("数据连接失败");
    mysqli_set_charset($link,"utf8");
    mysqli_select_db($link,"bbs");

    switch($_GET['a']){
        case 'add':
            $linkname = $_POST['linkname'];
            $content = $_POST['content'];
            $url = $_POST['url'];

            if(!empty($linkname) && !empty($content) && !empty($url) ) {
                $sql = "insert into friendlink (linkname,url,logo,content,ordernum) values('{$linkname}','{$url}','default.jpg','$content',1)";
                $result = mysqli_query($link, $sql);
                if ($result && mysqli_affected_rows($link) > 0) {
                    echo "<script>alert('添加成功！');window.location.href='./check.php';</script>";
                    die;
                } else {
                    echo "<script>alert('添加失败！');window.location.href='./addLink.php';</script>";
                    die;
                }
            }
            break;
        case 'del':
            $id = $_GET['id'];
//            var_dump($id);

            $sql = "delete from friendlink where id={$id}";
            $result = mysqli_query($link,$sql);
            if($result && mysqli_affected_rows($link)>0){
                echo "<script>alert('删除成功');window.location.href='./check.php';</script>";
                die;
            }else{
                echo "<script>alert('删除失败');window.location.href='./check.php';</script>";
                die;
            }
            break;
        case 'edit':
            $id = $_POST['id'];

            $linkname = $_POST['linkname'];
            $content = $_POST['content'];
            $url = $_POST['url'];

            if(!empty($linkname) && !empty($content) && !empty($url) ) {
                $sql = "update friendlink set linkname='{$linkname}',content='{$content}',url='{$url}' where id={$id}";
                $result = mysqli_query($link, $sql);
                if ($result && mysqli_affected_rows($link) > 0) {
                    echo "<script>alert('修改成功！');window.location.href='./check.php';</script>";
                    die;
                } else {
                    echo "<script>alert('修改失败！');window.location.href='./check.php';</script>";
                    die;
                }
            }else{
                echo "<script>alert('所有的条件都不能为空！');window.location.href='./check.php';</script>";
                die;
            }
            break;
    }

    mysqli_close($link);